package iosr11.Service;

import iosr11.contact.dao.ContactsHelper;
import iosr11.data.Event;
import iosr11.data.Group;
import iosr11.data.ProjectGroup;
import iosr11.data.Student;
import iosr11.data.UserGroup;
import iosr11.gbase.dao.StudentDAO;
import iosr11.gbase.dao.UserGroupDAO;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class GroupServiceImpl implements GroupService {

	private ContactsHelper contactsDAO;
	private StudentDAO studentDAO;
	private UserGroupDAO userGroupDAO;
	private Logger log = Logger.getLogger("service-log");

	
	
	@Override
	public Class<Group> getServiceEntityClass() {
		return Group.class;
	}

	@Override
	public boolean createGroup(String title) {
		Group group = new Group();
		group.setTitle(title);
		group.setStudents(null);
		return this.addGroup(group);
	}

	@Override
	public boolean addGroup(Group group) {
		log.info("dodawanie grupy");
		return contactsDAO.addGroup(group);
	}

	@Override
	public boolean deleteGroup(Group group) {
		// TODO brak usuwania gupy przy kontaktach
		
		
		log.info("usuwanie grupy");
		return contactsDAO.deleteGroup(group);
	}
	
	@Override
	public Group getGroup(String title) {
		log.info("wyszukiwanie grupy o nazwie: "+title);
		Group group = contactsDAO.getGroup(title);
		return group;
	}
	

	@Override
	public List<Group> getStudentGroups(Student student) {
		List <UserGroup> lista = userGroupDAO.getUserGroupForUser(student.getUser());
		if (lista==null){
			log.info("brak grup dla studenta");
			return null;
		}
		List<Group> result = new ArrayList<Group>();
		for (UserGroup ug : lista){
			Group tmp = contactsDAO.getGroupByRef(ug.getGroup().getId());
			if (tmp==null){
				log.info("blad w grupach studenta - przerwano");
				return null;
			}
			result.add(tmp);
		}
		log.info("znaleziono "+result.size()+" grup dla studenta");
		return result;
	}
	
/*
	@Override
	public List<ProjectGroup> getEventGroup(Event event) {
		// TODO Auto-generated method stub
		if (event==null){
			log.info("nie podano eventu");
			return null;
		}
		if (event.getSeries()==null){
			log.info("nie przypisano event serii - blad");
			return null;
		}
		StudentService studS = (StudentService) ServiceLocatorImpl.getInstance().getService(Student.class);
		List<Student> studenci = studS.getStudentsForEvent(event);
		if (studenci==null){
			log.info("brak studentow do eventu - brak grup");
			return null;
		}
		List <String> grpurl = new ArrayList<String>();
		for (Student stud : studenci){
			if (stud.getProjectGrp()!=null){
				if (stud.getProjectGrp().getId()!=null){
					if (!grpurl.contains(stud.getProjectGrp().getId())){
						grpurl.add(stud.getProjectGrp().getId());
					}
				}
			}
		}
		
		List <ProjectGroup>
		
		return null;
	}
*/
	
	@Override
	public boolean addStudentToGroup(Student student, Group group) {
		/*if (student==null){
			log.info("brak studenta -null");
			return false;
		}
		if (student.getUser()==null){
			log.info("brak user'a dla studenta -null");
			return false;
		}
		if (group==null){
			log.info("brak grupy -null");
			return false;
		}
		student.addGroup(group);
		if (!studentDAO.update(student)){
			log.info("nie dodano grupy - blad aktualizacji studenta");
			return false;
		}
		else if (!contactsDAO.updateUser(student.getUser())){
			log.info("nie dodano grupy - blad aktualizacji kontaktu");
			return false;
		}
		else {
			log.info("dodano grupe");
			return true;
		}*/
		// TODO
		if (student==null){
			log.info("brak studenta");
			return false;
		}
		if (student.getUser()==null){
			log.info("brak kontaktu dla studenta");
			return false;
		}
		if (group==null){
			log.info("brak grupy");
			return false;
		}
		UserGroup entity = new UserGroup(student.getUser(), group);
		return userGroupDAO.add(entity);
	}
	
	@Override
	public boolean removeStudentFromGroup(Student student, Group group) {
		if (student==null){
			log.info("brak studenta");
			return false;
		}
		if (student.getUser()==null){
			log.info("brak kontaktu dla studenta");
			return false;
		}
		if (group==null){
			log.info("brak grupy");
			return false;
		}
		List<UserGroup> lista = userGroupDAO.getUserGroupForUser(student.getUser());
		if (lista == null){
			log.info("brak grup studenta");
			return false;
		}
		
		for (UserGroup tmp : lista){
			if (tmp.getGroup().getId().equals(group.getId())){
				log.info("znaleziono powiazanie - usuwanie");
				return userGroupDAO.delete(tmp);
				
			}
		}
		log.info("zadna grupa studenta nie jest wlasciwa - nie usunieto");
		return false;
	}

	

	/* =========================================================
	 * Settery dla springa
	 * =========================================================
	 */
	

	public void setContactsDAO(ContactsHelper contactsDAO) {
		this.contactsDAO = contactsDAO;
	}

	public void setStudentDAO(StudentDAO studentDAO) {
		this.studentDAO = studentDAO;
	}

	public void setUserGroupDAO(UserGroupDAO userGroupDAO) {
		this.userGroupDAO = userGroupDAO;
	}

}
